package com.nip.irregularvebsdict.utilities;

import java.io.IOException;
import java.io.InputStream;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.sax.StartElementListener;
import android.util.Xml;

import com.nip.irregularvebsdict.items.IrregularVerbDetailsItem;

public class IrregularVerbsDetailsParser extends BaseParserFeed {

	public final static String WORD_TAG = "word";
	public static final String VALUE_ATT = "value";
	private RootElement rootElement;
	private IrregularVerbDetailsItem detailsItem = null;

	public IrregularVerbsDetailsParser(InputStream inputStream) {
		super(inputStream);
	}

	public IrregularVerbDetailsItem getIrregularVerbDetailsItem() {

		rootElement = new RootElement(WORD_TAG);

		rootElement.setStartElementListener(new StartElementListener() {

			@Override
			public void start(Attributes attributes) {
				detailsItem = new IrregularVerbDetailsItem();
				detailsItem.setWorks(attributes.getValue(VALUE_ATT));
			}
		});

		rootElement.setEndTextElementListener(new EndTextElementListener() {

			@Override
			public void end(String body) {
				detailsItem
						.setMean(body);
			}
		});

		try {
			Xml.parse(this.inputStream, Xml.Encoding.UTF_8,
					rootElement.getContentHandler());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return detailsItem;
	}
}
